package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.voclient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.handler.GuiliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-27
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {

  @Autowired
  private EduVideoService eduVideoService;

  @Autowired
  private voclient voclient;//注入调用

  @PostMapping("addvideo")
  public R addvideo(@RequestBody EduVideo eduVideo) {
    eduVideoService.save(eduVideo);
    return R.ok();
  }

  @DeleteMapping("{id}")
  public R delvideo(@PathVariable String id) {
    EduVideo eduVideo = eduVideoService.getById(id);
    String videoSourceId = eduVideo.getVideoSourceId();
    if (videoSourceId != null) {
      R result = voclient.removeAlyVideo(videoSourceId);
      if (result.getCode() == 20001) {
        throw new GuiliException(20001, "删除视频失败，熔断器...");
      }
    } //删除小节
    eduVideoService.removeById(id);
    return R.ok();
  }
}




